package com.gameShare.controller.user

import com.gameShare.annotation.VerifyCaptcha
import com.gameShare.controller.base.BaseController
import com.gameShare.entity.dto.user.UserBaseInfoDto
import com.gameShare.entity.po.user.UserInfo
import com.gameShare.entity.vo.ResponseVo
import com.gameShare.service.user.AccountService
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import jakarta.validation.constraints.Email
import jakarta.validation.constraints.NotEmpty
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@Validated
@RestController
@RequestMapping("/account")
open class AccountController(private val accountService: AccountService) : BaseController() {

    data class RegisterRequest(
        @field:Email
        @field:NotEmpty
        val email: String,
        @field:NotEmpty
        val password: String,
        @field:NotEmpty
        val nickName : String,
        val avatar : String,
        val introduction : String,
    )

    data class LoginRequest(
        @field:Email
        @field:NotEmpty
        val email : String,
        @field:NotEmpty
        val password : String
    )

    /**
     * post请求登录接口 要求账号和密码
     *
     * @param loginRequest 账号和密码
     * @return 用户信息
     */
    @VerifyCaptcha
    @PostMapping("/login")
    open fun login(request: HttpServletRequest, response : HttpServletResponse, @RequestBody @Validated loginRequest: LoginRequest): ResponseVo<UserBaseInfoDto> {
        val loginInfo  = UserInfo()
        loginInfo.email = loginRequest.email
        loginInfo.password = loginRequest.password
        val userInfo = accountService.login(response, request, loginInfo)
        return getSuccessResponseVO(userInfo)
    }

    /**
     *
     * 注册控制层
     * 并且用验证码保证不是机器人发送
     * @param registerRequest 注册的post请求
     * @return 空请求值对象
     */

    @VerifyCaptcha
    @PostMapping("/register")
    open fun register(request: HttpServletRequest, @RequestBody @Validated registerRequest: RegisterRequest): ResponseVo<Boolean> {

        val registerInfo  = UserInfo()
        registerInfo.email = registerRequest.email
        registerInfo.password = registerRequest.password
        registerInfo.nickName = registerRequest.nickName
        registerInfo.introduction = registerRequest.introduction
        registerInfo.avatar = registerRequest.avatar

        accountService.register(registerInfo)
        return getSuccessResponseVO(true)
    }
}